﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using HYFamilyLibDataManager;

namespace HYFamilyLibManager.FamilyLibModule
{
	public partial class DlgSearchByName : System.Windows.Forms.Form
	{
		public string FamilyName { get; set; }

		public FamilyLibDatabase Database { get; set; }

		public DlgSearchByName(List<FamilyLibDatabase> dbs)
		{
			this.mDbs = dbs;
			this.InitializeComponent();
			foreach (FamilyLibDatabase item in this.mDbs)
			{
				this.cbxSubject.Items.Add(item);
			}
			if (this.cbxSubject.Items.Count > 0)
			{
				this.cbxSubject.SelectedIndex = 0;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public static DlgSearchByName GetInstance(Form main, List<FamilyLibDatabase> dbs)
		{
			if (DlgSearchByName.dac == null || DlgSearchByName.dac.IsDisposed)
			{
				DlgSearchByName.dac = new DlgSearchByName(dbs);
				DlgSearchByName.dac.MdiParent = main.ActiveMdiChild;
			}
			else
			{
				DlgSearchByName.dac.mDbs = dbs;
				DlgSearchByName.dac.cbxSubject.Items.Clear();
				foreach (FamilyLibDatabase item in DlgSearchByName.dac.mDbs)
				{
					DlgSearchByName.dac.cbxSubject.Items.Add(item);
				}
				if (DlgSearchByName.dac.cbxSubject.Items.Count > 0)
				{
					DlgSearchByName.dac.cbxSubject.SelectedIndex = 0;
				}
			}
			return DlgSearchByName.dac;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.txtName.Text != "")
			{
				this.Database = (FamilyLibDatabase)this.cbxSubject.SelectedItem;
				this.FamilyName = this.txtName.Text;
				base.Close();
			}
		}

		private void DlgSearchByName_Load(object sender, EventArgs e)
		{
		}

		private static DlgSearchByName dac;

		private List<FamilyLibDatabase> mDbs = new List<FamilyLibDatabase>();
	}
}
